package com.dss.auto_deploy.controller;

import com.dss.auto_deploy.service.IAdmNginxService;
import com.dss.auto_deploy.dto.*;
import com.dss.auto_deploy.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.IOException;

/**
* nginx
* @Description:nginx控制层
* @author dongshoushan
* @date 2022-10-30 16:40:19
*/
@RestController
@RequestMapping("/admNginx")
@RequiredArgsConstructor(onConstructor = @_(@Autowired))
@Api(tags ="99: nginx")
public class AdmNginxController {

    /**
    * nginxService
    */
    private final  IAdmNginxService admNginxService;
    private final HttpServletRequest request;

    /**
    * 新增nginx
    * @param addDto
    * @return
    */
    @PostMapping
    @ApiOperation(value = "新增nginx", notes="新增nginx")
    public Result insert(@RequestBody @Valid AdmNginxAddDto addDto) {
        admNginxService.insert(addDto);
        return Result.success("新增成功");
    }

    /**
    * 修改nginx
    * @param updateDto
    * @return
    */
    @PutMapping
    @ApiOperation(value = "修改nginx", notes="修改nginx")
    public Result update(@RequestBody @Valid AdmNginxUpdateDto updateDto) {
        admNginxService.update(updateDto);
        return Result.success("修改成功");
    }

    /**
    * 批量删除nginx
    * @param ids
    * @return
    */
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除nginx", notes="删除nginx")
    public Result deleteBatch(@PathVariable Long[] ids) {
        admNginxService.deleteBatch(ids);
        return Result.success("删除成功");
    }

    /**
    * 分页查询nginx
    * @param pageDto
    * @return
    */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询nginx", notes="分页查询nginx")
    public Result page(AdmNginxPageDto pageDto) throws InterruptedException {
        return Result.success(admNginxService.list(pageDto));
    }

    @ApiOperation(value = "m安装", notes="安装")
    @PostMapping(value = "install", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Result install(
            @RequestParam(value = "ids") String ids,
            @RequestParam(value = "tempPath") String tempPath,
            @RequestPart(value = "nginxFile") MultipartFile nginxFile) throws Exception {
        request.getInputStream().skip(request.getContentLength());
        admNginxService.install(ids,tempPath,nginxFile);
        return Result.success();
    }
    @PostMapping("/restartService")
    @ApiOperation(value = "重启", notes="重启")
    public Result restartService(String ids) throws IOException {
        admNginxService.restartService(ids);
        return Result.success("重启成功");
    }

}
